package com.github.prontera.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.prontera.ApiVersion;
import com.github.prontera.RandomlyThrowsException;
import com.github.prontera.common.filter.CurrentUserId;
import com.github.prontera.domain.User;
import com.github.prontera.model.response.ObjectDataResponse;
import com.github.prontera.service.UserService;
import com.github.prontera.util.Page;

/**
 * 系统用户管理
 * @author Alex
 */
@RestController
@RequestMapping(value = ApiVersion.MANAGE_V1_PREFIX+"/user", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}, consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})
public class UserController {
	
    @Autowired
    private UserService userService;

    
    @RandomlyThrowsException
	@ApiOperation(value = "用户管理-用户列表", notes = "用户管理-用户列表")
	@RequestMapping(value = "/getUserList", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<User>> getUserList(@CurrentUserId Long userId,@RequestBody User user){
		Page<User> datas = userService.page(user);
    	return new ObjectDataResponse<Page<User>>(datas);
	}
    
	@ApiOperation(value = "用户管理-新增用户", notes = "用户管理-新增用户")
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> addUser(@RequestBody  User user) {
    	Integer datas = userService.insert(user);
    	return new ObjectDataResponse<Integer>(datas);
    }

	@ApiOperation(value = "用户管理-新增用户", notes = "用户管理-新增用户")
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> updateUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) {
		Integer datas = userService.updateUser(user);
    	return new ObjectDataResponse<Integer>(datas);
    }
    
	@ApiOperation(value = "用户管理-删除用户", notes = "用户管理-删除用户")
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> deleteUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		Integer res = userService.deleteUser(user);
		return new ObjectDataResponse<Integer>(res);
	}
	
	@ApiOperation(value = "用户管理-禁用用户", notes = "用户管理-禁用用户")
	@RequestMapping(value = "/banUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> banUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		Integer res = userService.banUser(user);
		return new ObjectDataResponse<Integer>(res);
	}
	@ApiOperation(value = "用户管理-启用用户", notes = "用户管理-启用用户")
	@RequestMapping(value = "/startUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> startUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		Integer res = userService.startUser(user);
    	return  new ObjectDataResponse<Integer>(res);
	}
    
	@ApiOperation(value = "用户管理-查看用户", notes = "用户管理-查看用户")
	@RequestMapping(value = "/getUser", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<User> getUser(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) {
		User bean = userService.getUser(user);
		return  new ObjectDataResponse<User>(bean);
	}
	
	@ApiOperation(value = "用户管理-根据账号查看用户", notes = "用户管理-根据账号查看用户")
	@RequestMapping(value = "/accountQuery", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Page<User>> accountQuery(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user) {
		Page<User> res = userService.accountQuery(user);
    	return new ObjectDataResponse<Page<User>>(res);
	}
	
	@ApiOperation(value = "用户管理-重置密码", notes = "用户管理-重置密码")
	@RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Integer> resetPassword(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody  User user){
		Integer res = userService.resetPassword(user);
		return new ObjectDataResponse<Integer>(res);
	}
	
	@ApiOperation(value = "首页统计数据信息", notes = "首页统计数据信息")
	@RequestMapping(value = "/statisticalInformation", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse <Map<String, Object>> statisticalInformation(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId){
        Map<String, Object> map =  userService.statisticalInformation(userId);
		return new ObjectDataResponse<Map<String, Object>>(map);
	}

}
